/**
 * Created by Administrator on 2017/7/5.
 */
let userDao = require('../mysql/userDao.js');
let rechargeDao = require('../mysql/rechargeDao.js');
let firstPaidDao = require('../mysql/firstPaidDao.js');
let msg = require('../util/msg.js');
let str = require('../util/str.js');
let variable = require('../data/variable.js');
let call = require('./call.js');
let gameData = require('../util/gameData.js');
let log = require('../util/log.js');

let pay = module.exports;

pay.callback = function (data) {
    console.log("pay.callback...");
    console.log(data);
    let body = JSON.parse(data);
    let rechargeModel = new rechargeDao();
    rechargeModel.inId(body.ordersId, function (err, res) {
        if (err != null) {
            return false;
        }   
        if(res.res.status!=0){
            console.log('已经处理过订单了》',res.res.status);
            return false;
        }   
        let cid = call.socket.getCid(res.res.userId);
        let uuid = call.socket.getUuid(res.res.userId);
        let type = gameData.d.goods[res.res.goodsId].type;
        let addCoin = gameData.d.goods[res.res.goodsId].coin + gameData.d.goods[res.res.goodsId].add;
        rechargeModel.set('status', 1);
        rechargeModel.set('ordersId', data.transactionId);
        rechargeModel.set('completeTime', new Date().Format("yyyy-MM-dd hh:mm:ss"));
        rechargeModel.set('tradeStatus', 0);
        rechargeModel.set('receipt', data.sign);
        rechargeModel.save(function (err1, res1) {
            if (err1 != null) {
                //console.error(err1);
                return false;
            }
            let firstPaidModel = new firstPaidDao();
            firstPaidModel.inGoodsId(res.res.userId, res.res.goodsId, function (err1, res1) {
                let coin = gameData.d.goods[res.res.goodsId].coin;
                let add = gameData.d.goods[res.res.goodsId].add;
                if (res1 == null) {
                    addCoin = addCoin * 2;
                    coin = coin * 2;
                    add = add * 2;
                    firstPaidModel.set('userId', res.res.userId);
                    firstPaidModel.set('goodsId', res.res.goodsId);
                    firstPaidModel.save(function () {
                    });
                }
                console.log("通知金币",res.res.userId, addCoin, type, cid, uuid);
                pay.setGold(res.res.userId, addCoin, type, cid, uuid);
                let addText = '';
                if (gameData.d.goods[res.res.goodsId].add > 0) {
                    addText = '，额外赠送' + add;
                }
                //msg.send(cid, uuid, 'funcRechargeFinish', {'addCoin': addCoin, 'type': type});
                msg.send(cid, uuid, 'globalAlert', {
                    'text': '充值成功，' + str[type] + '增加了' + coin + addText,
                    'title': null
                });
                log.p('充值成功，userId:' + res.res.userId);
            });
        });
    });
};
pay.setGold = function (userId, addCoin, type, cid, uuid) {
    let userModel = new userDao();
    userModel.inId(userId, function (err, res) {
        if (err != null) {
            return false;
        }
        let change = {};
        let coin = res.res[type] + addCoin;
        userModel.set(type, coin);
        change[type] = {'add': addCoin, 'now': coin};
        userModel.save(function (err1, res1) {
            if (err1 != null) {
                //console.error(err1);
                return false;
            }
            if (cid && uuid) {
                console.log("globalUserChange",change)
                msg.send(cid, uuid, 'globalUserChange', change);
            }
        });
    });
};

pay.closeOrder = function (data) {
    console.log(JSON.stringify(data));
    let rechargeModel = new rechargeDao();
    rechargeModel.inId(data.ordersId, function (err, res) {
        if (err != null) {
            return false;
        }   
        if(res==null){
            return false;
        }   
        rechargeModel.set('status', 2); 
        rechargeModel.set('id',data.ordersId);
        rechargeModel.save(function (err, res) {
            if (res != null) {
                //console.error(err);
                return false;
            }   
        });
    });
};
